package com.firepongo.awosome.client.controller;

import com.firepongo.awosome.api.data.ChaosUserData;
import com.firepongo.awosome.api.service.IChaosUserService;
import com.firepongo.chaos.model.data.UpdateData;
import com.firepongo.chaos.web.annotation.WxminiLoginToken;
import com.firepongo.chaos.web.base.BaseController;
import com.firepongo.chaos.web.dto.result.data.DataResult;
import com.firepongo.chaos.web.service.gaode.AmapService;
import com.firepongo.chaos.web.service.gaode.GaoDeAddress;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author G.G
 * @since 2020-11-17
 */
@Slf4j
@Api(tags = "ChaosUserController")
@RestController
@RequestMapping("/api/chaosUser")
public class ChaosUserController extends BaseController {
    @Autowired
    private IChaosUserService iChaosUserService;
    @Autowired
    private AmapService amapHelper;

    @WxminiLoginToken(needPhone = false)
    @PostMapping("/update")
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    public DataResult<Boolean> update(
            @ApiParam(value = "") @RequestBody @Validated UpdateData<ChaosUserData> update, BindingResult bindingResult, HttpServletRequest request) throws Exception {
        validate(bindingResult);
        if (!StringUtils.isEmpty(update.getData().getLon())) {
            GaoDeAddress address = amapHelper.regeo(update.getData().getLon(), update.getData().getLat());
            update.getData().setCity(address.getCity());
            update.getData().setProv(address.getProv());
            update.getData().setDist(address.getDist());
            update.getData().setAddr(address.getAddr());
        }
        update.setMu(getWxLoginUserMu(request));
        return dataResult(iChaosUserService.updateModelByMU(update));
    }


}
